const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    jd:'',
    upImg: [],//图片路径
    upImgId: [],//图片ID
    section_id:'',
    takContributeVal:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      jd: options.jd,
      section_id:options.section_id
    })
  },
  // 上传图片
  upImgBtn: function () {
    var _this = this;
    var { upImg, upImgId } = this.data
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success(res) {
        const tempFiles = res.tempFiles
        console.log(res)
        wx.showLoading({
          title: '加载中',
        })
        wx.uploadFile({
          url: app.globalData.baseUrl + 'fileUpload',
          filePath: tempFiles[0].path,
          name: 'file',
          formData: {
            token: app.globalData.token,
            file: tempFiles[0].path
          },
          success(upres) {
            wx.hideLoading()
            var obj = JSON.parse(upres.data)
            const data = upres.data.url
            console.log(obj)
            if (obj.code == 1000){
              upImg.push(obj.data.url)
              upImgId.push(obj.data.id)
              _this.setData({
                upImg: upImg,
                upImgId: upImgId
              })
              console.log(upImg,'upImg--------------')
            }else{
              wx.showToast({
                title: obj.message,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })

  },
  // 删除图片
  delImg: function (e) {
    var { upImg, upImgId } = this.data;
    var { index } = e.currentTarget.dataset
    upImg.splice(index, 1)
    upImgId.splice(index, 1)
    this.setData({
      upImg,
      upImgId
    })
  },

  taskDescriptionTap(){
    wx.navigateTo({
      url: '/userPage/pages/storeText/storeText?type=3&text='+this.data.takContributeVal,
    })
  },

  // 附件下载
  wxdown:function(event){
    var { down_url, annextype} = event.currentTarget.dataset;
    let _that = this;
    if (annextype != 'jpg' && annextype != 'png' && annextype != 'jpeg' && annextype != 'gif'){
      if (annextype != 'zip' && annextype != 'rar'){
        wx.downloadFile({
          url: down_url,
          success: function (res) {
            const tempFilePath = res.tempFilePath;
            // 保存文件
            wx.saveFile({
              tempFilePath,
              success: function (res) {
                const savedFilePath = res.savedFilePath;
                // 打开文件
                wx.openDocument({
                  filePath: savedFilePath,
                  showMenu:true,
                  success: function (res) {
                    console.log('打开文档成功')
                  },
                });
              },
              fail: function (err) {
                console.log('保存失败：', err)
              }
            });
          },
          fail: function (err) {
            console.log('下载失败：', err);
          },
        });
      }else{
        wx.showToast({
          title: '暂不支持改文件类型',
          icon: "none",
          duration: 2000
        })
      }
      return false;
    }else{
      wx.showToast({
        icon: 'loading',
        title: '正在保存图片',
        duration: 1000
      })
      //判断用户是否授权"保存到相册"
      wx.getSetting({
        success(res) {
          //没有权限，发起授权
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success() {//用户允许授权，保存图片到相册
                _that.savePhoto(down_url);
              },
              fail() {//用户点击拒绝授权，跳转到设置页，引导用户授权
                wx.openSetting({
                  success() {
                    wx.authorize({
                      scope: 'scope.writePhotosAlbum',
                      success() {
                        _that.savePhoto(down_url);
                      }
                    })
                  }
                })
              }
            })
          } else {//用户已授权，保存到相册
            _that.savePhoto(down_url);
          }
        }
      })
    }
  },


  // 稿件说明
  takContributeInp: function (e) {
    this.setData({
      takContributeVal: e.detail.value
    })
  },
  // 交稿
  submitContribute:function(){
    var _this = this
    var url = app.globalData.baseUrl + 'work/createDelivery';
    var { id, takContributeVal, upImgId, jd,section_id } = this.data;
    var data = {
      task_id: id,
      desc: takContributeVal,
      file_id: upImgId,
      sort: jd,
      token: app.globalData.token,
      section_id:section_id
    }
    wx.showLoading({
      title: '加载中',
    })
    app.wxPostRequest(url, data, (res) => {
      console.log(res)
      wx.hideLoading()
      if (res.code == 1000) {
        wx.showToast({
          title: '交稿成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          })
        },2000)
      }
    }, (err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.data.message,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
